/*
** this shader requires pixel/vertex shader 3_0, it uses a lot of texture loading operations
*/
char					g_shaders[] = ""
"sampler2D p;"
"typedef float f;"
"typedef float2 f2;"
"typedef float3 f3;"
"typedef float4 f4;"
"struct ist{"
	"f4 p:POSITION;"
	"f2 t:TEXCOORD0;"
"};"
"f pps(f2 i)"
"{"
	"f c=0;"
	"for(f s=16;s>=2;s/=2)c+=tex2D(p,i.xy*s).r/s*2;"
	"return c*0.5;"
"}"
"ist perlin_vs(f2 x:POSITION){"
	"ist o;"
	"o.t=f2(x.xy*.5);"
	"o.p=f4(x.xy,0,1);"
	"return o;"
"}"
"f4 perlin_ps(ist m):COLOR{"
	"f2 k=m.t;"
	"f g=pps(k),s=1.0/256.0;"
	"f r=pps(k+s*f2(1,0)),a=pps(k+s*f2(0,-1));"
	"f4 N=f4(normalize(f3(g-r,g-a,1)),g);"
	/*
	** You can use sobel as well (I chose the smallest code ;)
	*/
    //"f tl=abs(pps(k+s*f2(-1,-1)));"
    //"f l=abs(pps(k+s*f2(-1,0)));"
    //"f bl=abs(pps(k+s*f2(-1,1)));"
    //"f t=abs(pps(k+s*f2(0,-1)));"
    //"f b=abs(pps(k+s*f2(0,1)));"
    //"f tr=abs(pps(k+s*f2(1,-1)));"
    //"f r=abs(pps(k+s*f2(1,0)));"
    //"f br=abs(pps(k+s*f2(1,1)));"
    //"f dx=tr+2*r+br-tl-2*l-bl;"
    //"f dy=bl+2*b+br-tl-2*t-tr;"

    //"f4 N=f4(normalize(f3(dx,.125,dy)),g);
	"return N*.5+.5;"
"}"

"uniform float4x4 mwvp:register(c0);"
"uniform f2 e:register(c4);"
"ist sky_vs(ist v){"
	"ist o;"
	"f a=17000,c=4500,x=v.p.x,z=v.p.z,r=a*a-x*x-z*z;"
	"if(r<0)r=0;"
	"v.p.xz*=2;"
	"v.p.y=(c/a)*sqrt(r);"
	"o.p=mul(f4(v.p.xyz,1),mwvp);"
	"o.t=v.t+e*.5;"
	"return o;"
"}"
"f4 sky_ps(ist i):COLOR{"
	"f c=pps(i.t);"
	"return f4(c,c*.55,c*.4,0);"
"}"
"ist terr_vs(ist v){"
	"ist o;"
	"v.t+=e;"
	"v.p.xz*=1.5;"
	"v.p.y=9000*tex2Dlod(p,f4(v.t,0,0)).a-4000;"
	"f3 n=tex2Dlod(p,f4(v.t,0,0)).rgb;"
	"o.p=mul(f4(v.p.xyz,1),mwvp);"
	"o.t=v.t;"
	"return o;"
"}"
"f4 terr_ps(ist i):COLOR{"
"	f a=abs(sin(pps(i.t)*5.6));"
	"return f4(a,a*.55,a*.34,0);"
"}";